package rsexplorer.tree;

import java.util.Collections;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;

public class SortableTreeNode extends DefaultMutableTreeNode implements Comparable<SortableTreeNode>{

    protected boolean sortChildren;
             
    public SortableTreeNode(Object userObject) {
        this(userObject, true);
    }

    public SortableTreeNode(Object userObject, boolean sortChildren) {
        super(userObject);
        this.sortChildren = sortChildren;
    }

    public void setSortChildren(boolean sortChildren) {
        this.sortChildren = sortChildren;
    }

    public int compareTo(SortableTreeNode stn) {
        return toString().compareTo(stn.toString());
    }

    @Override
    public void insert(MutableTreeNode newChild, int childIndex) {
        super.insert(newChild, childIndex);
        if (sortChildren) Collections.sort(children); //probably not the most efficient...
    }
}
